<!--{include(file='head.html')}-->
<script type="text/javascript" src="template/jslib/date/WdatePicker.js"></script>
<div id="wrap">
	<!--{include('left.html')}-->
	<div id="maincont">
		<b class="b1"></b><b class="b2"></b><b class="b3"></b><b class="b4"></b>
		<form action="<!--{url('admin','discount','discountsave')}-->" method="post" name="submitform" id="submitform">
		<div id="main_c">
		<!--{if $opertype == "edit"}-->
			<ul class="bk">
				<li>
					<label><em>*</em>活动主题：</label>
					<p>
						<input class="input_tx" value="<!--{$discount.subject}-->" name="subject" id="subject" type="text" />
					</p>
				</li>
				<li>
					<label><em>*</em>活动时间：</label>
					<p>
						<input class="input_tx short" value="<!--{date('Y-m-d',$discount.begintime)}-->" name="begintime" id="begintime" type="text" onClick="WdatePicker({dateFmt:'yyyy-MM-dd'})"/>
						-
						<input class="input_tx short" value="<!--{date('Y-m-d',$discount.endtime)}-->" name="endtime" id="endtime" type="text" onClick="WdatePicker({dateFmt:'yyyy-MM-dd'})"/>
					</p>
				</li>
			</ul>

			<ul class="bk">
				<li>
					<label>商品：</label>
					<p>
						批量设置 限时折扣：<input type="text" class="input_tx mini" name="ndiscount" id="ndiscount"  value="10"/> 折 
							<input type="button" class="input_bt" value="设置"  onclick="ItemSelect.ndo()" />
					</p>
				</li>
			</ul>

			<table class="tableStyle" border="0" cellspacing="0" cellpadding="0">
				<thead>
					<tr align="center">
						<th width="10%">操作</th>
						<th width="60%">商品</th>
						<th width="10%">原价（￥）</th>
						<th width="10%">限时折扣</th>
						<th width="10%">折扣价（￥）</th>
					</tr>
				</thead>
				<tbody id="seltbody">
					<!--{foreach $discountitems item}-->
<tr align="center" id="tr_<!--{$item.itemid}-->">
	<td><a href="javascript:void(0);"  onclick="ItemSelect.del(<!--{$item.itemid}-->);return false;" class="del"></a></td>
	<td class="forimg">
		<a href="<!--{url('index','item','index','itemid=$item[itemid]')}-->" target="_blank">
			<img src = "<!--{$item.itemimg}-->_50x50.jpg" width="50" height="50" onerror="this.onerror='',this.src='images/nopicture.jpg'" class="mid"/>
		</a>
	</td>
	<td class="forprice"><!--{getPrice($item[price])}--></td>
	<td><input type='text' class='input_tx mini nd' value="<!--{getPrice($item.discount,-2,'int')}-->" 
			name='discount[key_<!--{$item.itemid}-->]' onkeyup='change(this)'/></td>
	<td class="fornewprice">
		<!--{getPrice($item[price] * $item[discount],-5)}-->
	</td>
</tr>
					<!--{/foreach}-->
				</tbody>
			</table>

			<ul class="bk">
				<li>
					<label></label>
					<p>
						<a href="javascript:void(0);" onclick="ItemSelect.toggle(this);return false;" class="bt_style2">展开</a>
					</p>
				</li>
			</ul>
			
			<ul class="bk none" id="outerframe">
				<li>
					<iframe src="about:blank" name="myiframe" id="myiframe" border="0" frameborder="0" width="100%" height="400" onload="this.height=myiframe.document.body.scrollHeight"></iframe>
				</li>
			</ul>
		<!--{elseif $opertype=="add"}-->
			<ul class="bk">
				<li>
					<label><em>*</em>活动主题：</label>
					<p>
						<input class="input_tx" value="" name="subject" id="subject" type="text" />
					</p>
				</li>
				<li>
					<label><em>*</em>活动时间：</label>
					<p>
						<input class="input_tx short" value="" name="begintime" id="begintime" type="text" onClick="WdatePicker({dateFmt:'yyyy-MM-dd'})"/>
						-
						<input class="input_tx short" value="" name="endtime"	id="endtime" type="text" onClick="WdatePicker({dateFmt:'yyyy-MM-dd'})"/>
					</p>
				</li>
			</ul>

			<ul class="bk">
				<li>
					<label>商品：</label>
					<p>
						批量设置 限时折扣：<input type="text" class="input_tx mini" name="ndiscount" id="ndiscount"  value="10"/> 折 
							<input type="button" class="input_bt" value="设置"  onclick="ItemSelect.ndo()" />
					</p>
				</li>
			</ul>

			<table class="tableStyle" border="0" cellspacing="0" cellpadding="0">
				<thead>
					<tr align="center">
						<th width="10%">操作</th>
						<th width="50%">商品</th>
						<th width="10%">原价（￥）</th>
						<th width="10%">限时折扣</th>
						<th width="10%">折扣价（￥）</th>
					</tr>
				</thead>
				<tbody id="seltbody">
				</tbody>
			</table>

			<ul class="bk">
				<li>
					<label></label>
					<p>
						<a href="javascript:void(0);" onclick="ItemSelect.toggle(this);return false;" class="bt_style2">选择商品</a>
					</p>
				</li>
			</ul>
			
			<ul class="bk none" id="outerframe">
				<li>
					<iframe src="about:blank" name="myiframe" id="myiframe" border="0" frameborder="0" width="100%" height="400" onload="this.height=myiframe.document.body.scrollHeight"></iframe>
				</li>
			</ul>
		<!--{/if}-->
			<div class="bt_bk2">
				<input type="hidden" value="<!--{$opertype}-->" name="opertype" />
				<input type="hidden" value="<!--{def($discountid)}-->" name="discountid"/>
				<a href="javascript:void(0);" class="btn" onclick="jssubmit('discount');return false;"><span>保存</span></a>
			</div>
		</div>
		</form>
		<b class="b4b"></b><b class="b3b"></b><b class="b2b"></b><b class="b1b"></b>
	</div>
</div>

<table class="none">
	<tbody id="foritem">
		<tr align="center" id="tr_{itemid}">
			<td><a href='javascript:void(0);' onclick="ItemSelect.del('{itemid}');return false;" class='del'></a></td>
			<td class="forimg"></td>
			<td class="forprice"></td>
			<td><input type='text' class='input_tx mini nd' value='10' name='discount[{itemid}]' onkeyup='ItemSelect.change(this)'/></td>
			<td class="fornewprice"></td>
		</tr>
	</tbody>
</table>
<!--{include(file='foot.html')}-->
<script type="text/javascript">
var ItemSelect = {
	tbody	 :$("#seltbody"),
	foritem	 :$("#foritem"),
	display  :false,
	numreg	 :/^[0-9]+.?[0-9]*$/,
	intreg	 :/^[0-9]+$/,
	toggle   :function(obj) {
		if(this.display) {
			$.oper.hideiframe();
			$(obj).text("选择商品");
		} else {
			$.oper.showiframe("<!--{url('admin','select','selectitem','type=iframe')}-->");
			$(obj).text("收起");
		}
		this.display = !this.display;
	},
	add:function(itemid) {
		var $frame	= $(window.frames["myiframe"].document),
			$html = this.foritem.clone(),
			price = parseFloat($("#price_"+itemid,$frame).text()).toFixed(2),
			img	  = $("#img_"+itemid,$frame).html();
		$html.find(".forimg").html(img);
		$html.find(".forprice,.fornewprice").html(price);
		this.tbody.append($html.html().replace(/{itemid}/g,itemid));
		$("#oper_"+itemid,$frame).html("<font color='red'>已添加</font>");
	},
	del:function(itemid) {
		var $frame = $(window.frames["myiframe"].document);
		$("#tr_"+itemid).remove();
		$("#oper_"+itemid,$frame).html("<a href='javascript:void(0);' class='select' onclick='window.parent.ItemSelect.add("+itemid+");return false;' ></a>");
	},
	change:function(obj) {
		var $obj = $(obj),val = $obj.val();
		var $parent   = $obj.parent(),
			$newprice = $parent.next(),
			$price	  = $parent.prev();
		if(!this.numreg.test(val)) {
			$obj.val('10');
			$newprice.text($price.text());
			return ;
		}
		var price	  = parseFloat( $price.text() ),
			newprice  = (price *  val / 10) . toFixed(2);
		$newprice.text(newprice);
	},
	ndo:function() {
		var $ndiscount	= $("#ndiscount"),
			ndiscount	= $ndiscount.val(),
			$nd			= this.tbody.find(".nd");
		if(!this.numreg.test(ndiscount)) {
			$ndiscount.val('10');
			return ;
		}
		ndiscount = parseFloat(ndiscount);
		$nd.val(ndiscount);
		$nd.each(function(){
			var $this	= $(this),
				$parent = $this.parent(),
				price	= parseFloat($parent.prev().text());
			$parent.next().text( (ndiscount * price / 10) .toFixed(2) );
		});
	}
}
</script>
